// 此文件用于存储用户相关信息
import { getInfo, emailVerify, setPwd, getEmail, verifyCode, updatePwd, setNewPwd, updateAvatar } from '@/api/user'
// import { toBlobUrl } from '@/utils/tools/downloadImg'
import download from '@/utils/tools/downloadImg'

const oldState = { // 用于重置state数据
  info: {
    // name: '刘有清',
    // email: '1964550834@qq.com',
    // tel: 15279140234,
    // identity: '管理员',
    // avatar: ''

  }

}

const state = JSON.parse(JSON.stringify(oldState))

const mutations = {
  set_info (state, data) {
    state.info = data
  }

}
const actions = {
  // 获取用户信息
  getUserInfo ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      getInfo(payload).then(async res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          if (res.data.data.adminAvatar) {
            res.data.data.imgSrc = await download.toBlobUrl(res.data.data.adminAvatar)
          } else {
            res.data.data.imgSrc = null
          }
          // res.data.data.imgSrc = await download.toBlobUrl(707)
          // console.log(res.data.data)
          commit('set_info', res.data.data)
          resolve(res.data)
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  // 新增后台人员-邮件-连接验证-是否失效 格式是否正确
  emailVerifyData ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      emailVerify(payload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          // commit('set_info', res.data.data)
          resolve(res.data)
        } else {
          reject(res.data)
        }
      })
    })
  },

  // 设置密码
  setPwdData ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      setPwd(payload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          // commit('set_info', res.data.data)
          resolve(res.data)
        } else {
          reject(res.data)
        }
      })
    })
  },

  // 获取邮箱验证码
  getEmailData ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      getEmail(payload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
        // commit('set_info', res.data.data)
          resolve(res.data)
        } else {
          reject(res.data)
        }
      })
    })
  },
  // 对验证码进行验证
  verifyCodeData ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      verifyCode(payload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          // commit('set_info', res.data.data)
          resolve(res.data)
        } else {
          reject(res.data)
        }
      })
    })
  },

  // 修改密码
  updatePwdData ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      updatePwd(payload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          // commit('set_info', res.data.data)
          resolve(res.data)
        } else {
          reject(res.data)
        }
      })
    })
  },
  // 设置新密码--忘记密码时
  setNewPwdData ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      setNewPwd(payload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          // commit('set_info', res.data.data)
          resolve(res.data)
        } else {
          reject(res.data)
        }
      })
    })
  },
  // 修改头像
  updateAvatarData ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      updateAvatar(payload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          // commit('set_info', res.data.data)
          resolve(res.data)
        } else {
          reject(res.data)
        }
      })
    })
  }

}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
